import { randomString, isNull, BackendServices, AppServices } from "mmb";
import {
  getBilinAdminVendorProxySafely,
  getMetadata,
  getOptionsOfLight,
  wrapperGrpcError,
} from "../grpc-client/grpcAdapter";
import * as bilinadminpb from "../grpc-client/proxy/bilinadmin_pb";

export const listCosBucketSets = async (app: AppServices): Promise<bilinadminpb.ListCosBucketSetsReply> => {
  const cl = getBilinAdminVendorProxySafely();
  const req = new bilinadminpb.ListCosBucketSetsReq();

  req.setApp(app);
  
  return new Promise((resolve, reject) => {
    cl.listCosBucketSets(req, getMetadata(), getOptionsOfLight(), (err, res) => {
      if (!isNull(err)) {
        reject(wrapperGrpcError(err.details));
        return;
      }
      resolve(res);
    });
  });
}

export const getCosAuthorization = async (bugPrefix: string): Promise<bilinadminpb.GetCosAuthorizationReply> => {
  const cl = getBilinAdminVendorProxySafely();
  const req = new bilinadminpb.GetCosAuthorizationReq();

  req.setBucketPrefix(bugPrefix);
  
  return new Promise((resolve, reject) => {
    cl.fCCGetCosAuthorization(req, getMetadata(), getOptionsOfLight(), (err, res) => {
      if (!isNull(err)) {
        reject(wrapperGrpcError(err.details));
        return;
      }
      resolve(res);
    });
  });
}